package cn.com.surker.base.strength.service.impl;

import cn.com.surker.base.strength.entity.BaseStrengthSpecial;
import cn.com.surker.base.strength.dao.BaseStrengthSpecialDao;
import cn.com.surker.base.strength.service.StrengthSpecialBaseService;
import cn.com.surker.common.MessageCenterException;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.util.StringUtils;
import cn.com.surker.vo.sale.SaleStrengthSpecialPriceVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* <p>
    * 强度特性基础表 服务实现类
    * </p>
*
* @author admin
* @since 2024-08-01
*/
public abstract class StrengthSpecialBaseServiceImpl extends ServiceImpl<BaseStrengthSpecialDao, BaseStrengthSpecial> implements StrengthSpecialBaseService {

    @Resource
    protected BaseStrengthSpecialDao strengthSpecialDao;

    /**
     * 分页查询强度特性
     * @return
     */
    public IPage findPage(String searchVal, Integer type, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        QueryWrapper<BaseStrengthSpecial> query = new QueryWrapper<>();
        if (!StringUtils.isNull(searchVal)) {
            query.and(orderWrapper -> orderWrapper.like("special", searchVal).or()
                    .like("remark", searchVal));
        }
        if (!StringUtils.isNull(type))
            query.eq("type", type);
        query.eq("status", 0);
        query.orderByDesc("id");

        IPage<BaseStrengthSpecial> iPage = strengthSpecialDao.selectPage(new Page<>((offset + limit) /limit, limit), query);
        /**分页插件**/
        if (!StringUtils.isNull(iPage.getRecords())) {
            PageInfo pageInfo = new PageInfo(iPage.getRecords());
            if (!StringUtils.isNull(pageInfo.getTotal()))
                iPage.setTotal(pageInfo.getTotal());
        }
        return iPage;
    }

    /**
     * 新增
     * @param strengthSpecial
     * @return
     */
    public RestResultDto saveSpecial(BaseStrengthSpecial strengthSpecial) {
        if (strengthSpecial == null || StringUtils.isAllParamNull(strengthSpecial.getSpecial(), strengthSpecial.getType()))
            throw new MessageCenterException("参数错误");

        String special = strengthSpecial.getSpecial().toUpperCase().replaceAll(" ", "");
        strengthSpecial.setSpecial(special);
        QueryWrapper<BaseStrengthSpecial> query = new QueryWrapper<>();
        query.eq("status", 0);
        query.eq("special", special);
        Integer total = strengthSpecialDao.selectCount(query);
        if (total > 0)
            throw new MessageCenterException(special + "标号特性已存在");
        strengthSpecial.setCreateTime(new Date());
        Integer code = strengthSpecialDao.insert(strengthSpecial);
        if (code > 0)
            return RestResultDto.success("新增成功");
        throw new MessageCenterException("新增失败");
    }

    /**
     * 禁用特性
     * @param id
     * @param creator
     * @return
     */
    public RestResultDto updateStatus(Long id, Integer status, String creator) {
        if (StringUtils.isAllParamNull(id, creator))
            throw new MessageCenterException("参数错误");
        BaseStrengthSpecial special = strengthSpecialDao.selectById(id);
        if (special == null || special.getStatus() == 1)
            throw new MessageCenterException("标号特性不存在或已被删除");
        special.setUpdateTime(new Date());
        special.setUpdator(creator);
        special.setStatus(status);

        Integer code = strengthSpecialDao.updateById(special);
        if (code > 0)
            return RestResultDto.success("更新成功");
        throw new MessageCenterException("更新失败");
    }

    /**
     * 根据类型查询等级
     * @param type
     * @return
     */
    public List<BaseStrengthSpecial> findSpecial(Integer type) {
        QueryWrapper<BaseStrengthSpecial> query = new QueryWrapper<>();
        query.eq("status", 0);
        if (!StringUtils.isNull(type))
            query.eq("type", type);
        query.orderByAsc("type");
        List<BaseStrengthSpecial> list = strengthSpecialDao.selectList(query);
        if (list == null || list.size() == 0)
            return null;
        return list;
    }

	@Override
	public List<SaleStrengthSpecialPriceVo> findByConId(String dbCode, Long orgId, Long saleConId) {
		if (dbCode == null) {
			return null;
		}
		List<SaleStrengthSpecialPriceVo> strengthSpecialList = strengthSpecialDao.findByConId(dbCode, orgId, saleConId);
		return strengthSpecialList;
	}
}